<template>
  <div style="height:100%;background-color: #F1F2F6;box-sizing: border-box;overflow: auto">
    <div class="login-container ">
      <div class="head">
        <h1 class="title1">客户数据后台管理系统</h1>
        <h3 class="title2"> 经理登录</h3>
      </div>
      <div class="main">
        <h2 class="top">账户密码登录</h2>
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="on"
          label-position="left"
        >
  
          <el-form-item prop="account">
            <span class="svg-container">
              <svg-icon icon-class="seller" />
            </span>
            <el-input
              ref="account"
              v-model="loginForm.account"
              placeholder="请输入用户账号"
              name="account"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>
          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入账号密码"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span
              class="show-pwd"
              @click="showPwd"
            >
              <svg-icon
                :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
              />
            </span>
          </el-form-item>
        </el-form>
        <div class="footer">
          <el-button
            :loading="loading"
            type="primary"
            style="width:100%;margin-bottom:1.875rem;"
            @click.native.prevent="handleLogin"
          >登录</el-button>
    
          <div class="tips">
            <el-checkbox v-model="remember">自动登录</el-checkbox>
            <div class="links">
              <el-link type="primary">忘记密码</el-link>
              <el-link type="primary">注册账户</el-link>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script >
import { validUsername } from '@/utils/validate'
import { thisTypeAnnotation } from '@babel/types'
export default {
  name: 'Login',
  data () {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的账号'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于六个数字'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        account: 'manage',
        password: '123456'
      },
      loginRules: {
        account: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      remember: false
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd () {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
            console.log('登陆成功');
          }).catch(() => {
            this.loading = false
            console.log('error submit!!')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
}
</script>

<style>
.login-container {
  margin: auto auto;
  min-height: 370px;
  height: 100%;
  min-width: 280px;
  width: 30rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.login-container .head {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.login-container .head h1{
  font-size: 2rem;
}
.login-container .head h3{
  font-size: 1rem;
  color: #8A8B8D;
}
.login-container .main{
  display: flex;
  flex-direction: column;
  align-items: center;
}
.login-container .main h2{
  font-size: 1.5rem;
  color: #1890FF;
}
.login-container .main .top{
  text-align: center;
  width: 13rem;
  font-weight: 520;
  padding-bottom: .625rem;
  border-bottom: .125rem solid #1890FF;
}
.login-container .el-form{
  width: 100%;
  display: flex;
  flex-direction: column;
}
.login-container .el-form-item{
  background: white;
   border: .0625rem solid #D9D9D9;
   border-radius: .375rem;
}
.login-container .el-input {
    display: inline-block;
    width: 85%;
}
.login-container .el-input input{
  border: 0;
}
.svg-container{
    padding: .375rem .25rem .375rem .9375rem;
    vertical-align: middle;
    width: 10%;
    display: inline-block;
    color: #889aa4;
}
.show-pwd {
    position: absolute;
    right: .625rem;
    top: .4375rem;
    font-size: 1rem;
    cursor: pointer;
    user-select: none;
  }
  .footer{
  width: 100%;
  margin-bottom: .625rem;
}
.tips{
  display: flex;
  justify-content: space-between;
}
.tips .links .el-link{
  margin-left: .625rem;
}
</style>